package com.cskaoyan.javase.homework.day22;

/**
 * 启动2个线程 打印100个数 (使用wait notify) 线程a 打印 1 3 5 7 9 ...奇数 线程b 打印 2 4 6 8 10 ...偶数 分析：线程a打印的是奇数 线程b打印的是偶数 (交替打印)
 *
 * 附加(选做): 怎么保证某个线程先打印, 比如
 * b先打印2
 * a打印1
 * b打印4
 * a打印3.......一直到100
 *
 * @since 06:36
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Work1 {
    private static final Object LOCK = new Object();

    public static void main(String[] args) {
        // print();

        print2(true);
    }

    public static void print2(boolean flag) {
        new Thread(() -> {
            synchronized (LOCK) {
                // 通过设定一个flag参数,为true时让A线程后执行,B线程先执行
                if (flag) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 1; i <= 100; i += 2) {
                    LOCK.notify();
                    System.out.println(Thread.currentThread().getName() + "打印了" + i);
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 唤醒对方
                LOCK.notify();
            }
        }, "A线程").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            synchronized (LOCK) {
                for (int i = 2; i <= 100; i += 2) {
                    LOCK.notify();
                    System.out.println(Thread.currentThread().getName() + "打印了" + i);
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 唤醒对方
                LOCK.notify();
            }

        }, "B线程").start();
    }


    public static void print() {
        new Thread(() -> {
            for (int i = 1; i <= 100; i += 2) {
                synchronized (LOCK) {
                    LOCK.notify();
                    System.out.println(Thread.currentThread().getName() + "打印了" + i);
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 唤醒对方
                    LOCK.notify();
                }
            }

        }, "A线程").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            for (int i = 2; i <= 100; i += 2) {
                synchronized (LOCK) {
                    LOCK.notify();
                    System.out.println(Thread.currentThread().getName() + "打印了" + i);
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 唤醒对方
                    LOCK.notify();
                }
            }

        }, "B线程").start();
    }
}
